#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 022

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.string_functions import alphabetical_value

def get_answer():
    """Question:
    
    
    Using names.txt, a 46K text file containing over five-thousand first names, 
    begin by sorting it into alphabetical order. Then working out the 
    alphabetical value for each name, multiply this value by its alphabetical 
    position in the list to obtain a name score.
    
    For example, when the list is sorted into alphabetical order, 
    COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name 
    in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
    
    What is the total of all the name scores in the file?    
    """
    #Get the list of names and sort it alphabetically.
    name_list = open("../data/022.txt", "r").readline() \
                    .replace("\"", "") \
                    .split(",")
                    
    name_list.sort()
    
    #Return result.    
    return sum(
              alphabetical_value(name_list[index]) * (index + 1)
                for index in xrange(len(name_list))
            )
    

    
if __name__ == "__main__":
    cProfile.run("print(get_answer())")
